/**
 * Copyright(c) 2006-2008, FeyaSoft Inc.
 */
package("feyaSoft.demouser");
 
/**
 * This JS is mainly used to handle action in create new 
 * user
 *
 * @author fzhuang
 * @Date Oct 7, 2007
 */
feyaSoft.feedback.CreateFeedback = function(dataStore, parentId, feedbackDS) {
    var title = 'Post New Feedback';

    // pre-define fields in the form
	var subject = new Ext.form.TextField({
        fieldLabel: 'Subject',
        allowBlank: false,
        name: 'subject',
        anchor: '90%' 
    });
    
	var name = new Ext.form.TextField({
        fieldLabel: 'Your name',
        allowBlank: false,
        name: 'author',
        anchor: '90%'  
    });  
    
	var email = new Ext.form.TextField({
	    vtype: 'email',
        fieldLabel: 'Email', 
        allowBlank: false, 
        blankText: 'Your Email will not be disclosed to any 3rd-party. We only use it to identify correct author when you edit your own feedback.',         
        name: 'email',
        anchor: '90%'     
    });  
    
    var desc = new Ext.form.HtmlEditor({
        xtype: 'htmleditor',
        xtype: 'textarea',
        hideLabel: true,
        id: 'description',
        anchor: '100% -35'  // anchor width by percentage and height by raw adjustment 
    }); 

    var formPanel = new Ext.form.FormPanel({
  
        baseCls: 'x-plain',
        labelWidth: 75,
        url:'feedback/listFeedback.htm?action=createFeedback',
        defaultType: 'textfield',

        items: [
            subject, 
            name,
            email,
            desc   
		]
    });
    
    // if parentId exist now.... it is comment ....
    if (parentId > 0) {
        title = 'Add New Comment';
    } else {
        parentId = '0';
    }

    // define window and show it in desktop
    var window = new Ext.Window({
        title: title,
        width: 600,
        height: 400,
        minWidth: 350,
        minHeight: 300,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        items: formPanel,

        buttons: [{
            text: 'Save', 
            handler: function() {
                // check form value 
                if (formPanel.form.isValid()) {
	 		        formPanel.form.submit({	
	 		            params:{parentId : parentId},		      
			            waitMsg:'In processing',
			            failure: function(form, action) {
						    Ext.MessageBox.alert('Error Message', action.result.errorInfo);
						},
						success: function(form, action) {
						    Ext.MessageBox.alert('Confirm', action.result.info);
						    window.hide();
						    dataStore.load({params:{start:0, limit:10}});
						    
						    if (feedbackDS != null) {
						        feedbackDS.load({params:{start:0, limit:10}});
						    }
						}
			        });                   
                } else{
					Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
				}             
	        }
        },{
            text: 'Cancel',
            handler: function(){window.hide();}
        }]
    });

    window.show();
};
